﻿using System.Collections.Generic;
using System.Linq;
using WMSSyncData.Model;
using WMSSyncData.Utils;

namespace WMSSyncData.DAL.SqlDB
{
    public class Tddev122DAL : BaseSqlDBDAL<tddev122>
    {
        public List<tddev122> GetFBAdjustmentData()
        {
            var sql = @"select a.BillNo as wmsno,b.RowNo as wmspo,' ' as orno,a.Storage_SN as cwar,'2514' as adrn,b.PartNumber as item,b.Unit as stun,b.RealQty as qadj,' ' as loca,' ' as clot,a.RelatedBill as ERPPO,
'1' as host,' ' as mess,GETDATE() as edte1,GETDATE() as wdte1,GETDATE() as wdte2,'0' as REFCNTD,'0' as REFCNTU
from wms_T_InStockRM a 
LEFT JOIN wms_T_InStockRMRows b on a.BillNo=b.BillNo 
where a.status='1' and a.BillType='分包入库'";
            var result = SqlDBHelper.GetDataSet(sql).Tables[0];
            var list = MakeTablePackage(result);
            return list;
        }

        public bool UpdateUploadStatus(List<tddev122> list)
        {
            var result = false;
            var billNoList = list.Select(x => x.wmsno).Distinct();
            if (billNoList.Count() > 0)
            {
                var sqlWhere = "where BillNo in (";
                foreach (var item in billNoList)
                {
                    sqlWhere = sqlWhere + "'" + item + "',";
                }
                sqlWhere = sqlWhere.TrimEnd(',');
                sqlWhere = sqlWhere + ")";
                var sql = "UPDATE  wms_T_InStockRM SET Status='2'" + sqlWhere + ";";
                var updateRes = SqlDBHelper.ExcuteSQL(sql);
                if (updateRes > 0)
                {
                    result = true;
                }
            }
            return result;
        }
    }
}
